package vn.ttpcode.com.sample.videoview;

import java.io.File;

import vn.ttpcode.com.sample.videoview.MyMediaController.MediaCtrListener;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.VideoView;

public class CustomVideoView extends VideoView{
	private static final String TAG = "ttpcode";
	
	private MyMediaController ctlr;
	private Context context;
	private RelativeLayout ctrTop;
	
	public CustomVideoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
	}
	
	public CustomVideoView(Context context, RelativeLayout ctrTop) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.ctrTop = ctrTop;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	public void onStart(){
		Log.d(TAG, "onStart - onStart");
		this.start();
	}
	
	public void setVideoPathExt(String videoPath){
		Log.d(TAG, "Video reader form Extension: " + videoPath);
		
		File file = new File(Environment.getExternalStorageDirectory(), videoPath);
		this.setVideoURI(Uri.fromFile(file));
	}
	
	public void setVideoControl(){
		ctlr = new MyMediaController(context);
		ctlr.setMediaPlayer(this);
		ctlr.setImplements(new MediaCtrListener() {
			
			@Override
			public void doStatus(final boolean isShow) {
				// TODO Auto-generated method stub
				
				((Activity)context).runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						if(isShow){
							ctrTop.setVisibility(View.VISIBLE);
						}else{
							ctrTop.setVisibility(View.GONE);
						}
					}
				});
				
				
			}
		});
		this.setMediaController(ctlr);
		
	}
	
	
	/**
	 * Pause video
	 */
	public void onPause(){
		this.pause();
	}
	
	public void onStop(){
		this.pause();
		this.setOnCompletionListener(null);
		this.stopPlayback();
	}
}
